package cn.tjnext.apply.dao;

import cn.tjnext.apply.contants.EntityStatus;
import cn.tjnext.apply.entity.ApplicantEntity;
import cn.tjnext.apply.repositories.ApplicantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by cp on 2017/8/4.
 */
@Component
public class ApplicantDao extends AbstractEntityDao<ApplicantEntity, ApplicantRepository> {

    @Autowired
    private ApplicantRepository applicantRepository;

    @Override
    protected ApplicantRepository getRepository() {
        return applicantRepository;
    }

    @Override
    public Sort getDefaultSorting() {
        return ApplicantEntity.DEFAULT_SORTING;
    }

    @Override
    public Page<ApplicantEntity> searchByKeyword(String keyWord, Pageable pageable) {
        return getRepository().findByUser_NameLikeAndStatus("%" + keyWord + "%", EntityStatus.NORMAL, pageable);
    }

    @Override
    public Page<ApplicantEntity> findAllOrderByDefault(Pageable pageable) {
        Pageable newPageable = addDefaultSortToPageable(pageable);
        return getRepository().findByStatus(EntityStatus.NORMAL, newPageable);
    }

    public List<ApplicantEntity> findByUserId(Long userId) {
        return getRepository().findByUser_pkIdAndStatus(userId, EntityStatus.NORMAL);
    }

    public List<ApplicantEntity> findAllByStatus() {
        return getRepository().findAllByStatus(EntityStatus.NORMAL);
    }

    public List<ApplicantEntity> findByOrderId(Long orderId) {
        return getRepository().findByOrderId(orderId);
    }
}
